﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Web.View.CustomerView
{
    public partial class SubscriptionList : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Bind car data to gridview
            BindCustomerSubscription(db);

            // Dispose connection to database
            db.Dispose();
        }


        #region Private Functions

        /// <summary>
        /// Function that bind customer cars to gridview
        /// </summary>
        /// <param name="db">Connection to database</param>
        private void BindCustomerSubscription(TireServiceDB db)
        {
            // Bind to gridview
            gvCustomerSubscription.DataSource = db.GetCustomerSubscriptionViews();
            gvCustomerSubscription.DataBind();
        }

        /// <summary>
        /// Function that change grouping in order gridview
        /// </summary>
        /// <param name="ViewIndex">Integer setting the grouping mode</param>
        protected void GroupOrderData(int ViewIndex)
        {
            gvCustomerSubscription.ClearSort();
            gvCustomerSubscription.GroupSummary.Clear();
            switch (ViewIndex)
            {
                case 0:
                    // Nothing - clear grouping

                    break;
                case 1:
                    gvCustomerSubscription.GroupBy(gvCustomerSubscription.Columns["SubscriptionDescription"]);

                    break;
                //case 2:
                //    gvOrder.GroupBy(gvOrder.Columns["CustomerNumber"]);

                //    break;
                //case 3:
                //    gvOrder.GroupBy(gvOrder.Columns["PhotoDate"]);
                //    gvOrder.GroupBy(gvOrder.Columns["CustomerNumber"]);

                //break;
            }
            gvCustomerSubscription.ExpandAll();
        }

        #endregion

        #region Control Functions

        /// <summary>
        /// Button function for export of gridview
        /// </summary>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (ddlExport.Value == "0")
            {
                gvExport.WritePdfToResponse();
            }

            if (ddlExport.Value == "1")
            {
                gvExport.WriteXlsToResponse();
            }

            if (ddlExport.Value == "2")
            {
                gvExport.WriteCsvToResponse();
            }

            if (ddlExport.Value == "3")
            {
                gvExport.WriteRtfToResponse();
            }
        }
        
        /// <summary>
        /// Dropdown list function that change gridview grouping when selected item is changed
        /// </summary>
        protected void ddlView_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Group data
            GroupOrderData(Convert.ToInt32(ddlView.SelectedValue));
        }

        #endregion

        #region GridView Functions

        /// <summary>
        /// Gridview function that change field data for unbound fields
        /// </summary>
        protected void gvCustomerSubscription_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
        {
            // Format UnBoundColumn SubscriptionDescription: Show subscription description
            if (e.Column.FieldName == "SubscriptionDescription")
            {
                // Get subscriptionid
                int subscriptionid = Convert.ToInt32(e.GetListSourceFieldValue("SubscriptionId"));

                TireServiceDB db = new TireServiceDB();

                // Change field value
                e.Value = db.GetSubscriptionDescription(subscriptionid);

                db.Dispose();
            }

            //// Format UnBoundColumn CurrencyCodeValue: Show currencycode
            //if (e.Column.FieldName == "CurrencyCodeValue")
            //{
            //    // Get currencyid
            //    int currencyid = Convert.ToInt32(e.GetListSourceFieldValue("CurrencyId"));

            //    TireServiceDB db = new TireServiceDB();

            //    // Change field value
            //    e.Value = db.GetCurrencyCode(currencyid);

            //    db.Dispose();
            //}
        }

#endregion
    
    }
}